<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core\lang;
use classlib\core as C;
use classlib\core\controller;
use src\logic\workflow\doc_executor;
class Capply extends controller {
	//申请表单信息列表
	public function main() {
		$view = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$dlang = lang::inst('dd');
		$view->assign('status', $dlang->get('sh0006'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('workflow/workflow.ext.js');
	}

	/**
	 * @desc    工作流复制用于版本开发
	 * @doc     工作流复制
	 * @access  需要授权
	 */
	public function docopy() {
		$view  = C\view::inst('json');
		Cadmin::chkaccess($view);
		$lang  = C\lang::inst('adm');
		$id    = (int)input::post('id');
		$worm  = C\model::load('main.proc.workflow');
		$data  = $worm->get_one($id);//Copy 工作流程
		if (empty($data)) {
			$view->assign_error('50016', $lang->get('workflow', '50016'));
		}
		$data['id']    = 0;
		$data['name'] .= '[Copy]';
		$data['stime'] = C\config::$nowtime;
		$newid = $worm->new_one($data);
		if ($newid < 1) {
			$view->assign_error('50017', $lang->get('workflow', '50017'));
		}
		$norm  = C\model::load('main.proc.node');//Copy 工作流节点信息
		$where = array('workflowid'=>$id, 'preid'=>0);
		$node  = $norm->get_item($where);
		$preid = 0;
		while ($node) {
			$where['preid']     = $node['id'];
			$node['id']         = 0;
			$node['workflowid'] = $newid;
			$node['preid']      = $preid;
			$preid = $norm->new_one($node);
			$node  = $norm->get_item($where);
		}
		$view->display();
	}

	/**
	 * @desc    工作流禁用,禁用之后将不能使用该流程
	 * @doc     工作流禁用
	 * @access  需要授权
	 */
	public function dodelete() {
		$view  = C\view::inst('json');
		Cadmin::chkaccess($view);
		$id    = (int)input::post('id');
		$where = array('workflowid'=>$id);
		$exist = C\model::load('main.proc.execution')->isexist($where);
		if ($exist) {
			$data['status']  = input::post('status');
			$data['status']  = ($data['status'] > 0)? 1:0;
			C\model::load('main.proc.workflow')->save($id, $data);
		} else {
			C\model::load('main.proc.workflow')->delete($id);
			$where = array('workflowid'=>$id);
			C\model::load('main.proc.node')->get_orm()->where('workflowid', $id)->delete();
			C\model::load('main.proc.node')->set_ver();
		}
		$view->display();
	}

	/**
	 * @desc    工作流列表,要申请什么工作流可以看这里
	 * @doc     工作流列表
	 * @access  需要授权
	 */
	public function dolist() {
		$view  = C\view::inst('json');
		Cadmin::chkaccess($view, 'workflow/apply');
		list($start, $limit, $sort) = Cadmin::offset();
		$where = array();
		$name  = input::post('name');
		if(!empty($name)) {
			$where[]= array('name', '%'.$name.'%', \classlib\orm::OP_LIKE);
		}
		$status= input::post('status');
		if (is_numeric($status)) {
			$where[]= array('status', $status, \classlib\orm::OP_EQ);
		}
		$orm   = C\model::load('main.proc.workflow');
		$total = $orm->get_total($where);
		$list  = array();
		if ($total > 0) {
			$list = $orm->get_list($start, $limit, $where, '*', $sort);
			foreach ($list as &$item) {
				$item['stime'] = doc_executor::datetime($item['stime']);
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
}
